package com.mathlearning.auth.repository;

import com.mathlearning.auth.entity.LearningProgress;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface LearningProgressRepository extends JpaRepository<LearningProgress, Long> {
    List<LearningProgress> findByUserIdAndCourseId(Long userId, Long courseId);
    Optional<LearningProgress> findByUserIdAndVideoId(Long userId, Long videoId);
    boolean existsByUserIdAndCourseId(Long userId, Long courseId);

    @Query("select count(p) from LearningProgress p where p.userId = ?1 and p.courseId = ?2 and p.completed = true")
    Long countCompletedVideos(Long userId, Long courseId);

    @Query("select p from LearningProgress p where p.userId = ?1 and p.courseId = ?2 order by p.lastWatchTime desc")
    List<LearningProgress> findLastWatchedVideo(Long userId, Long courseId);

    @Query("select p from LearningProgress p where p.userId = ?1 and p.courseId = ?2 and (p.completed = false or p.completed is null) order by p.lastWatchTime asc")
    List<LearningProgress> findFirstIncompleteVideo(Long userId, Long courseId);
}




